<?php


require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

//get query dan variabel
$awal = $_GET['awal'];
$akhir = $_GET['akhir'];
$jumlah = $akhir - $awal;
$width = ($jumlah+1)*3;

//instansiasi fpdf
$pdf = new PDF_MC_Table('P','cm','A4');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.7,0.7,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(20,2, "DANA UNIT PER TAHUN", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(1.5,3);
$pdf->Cell(8,3, "PERIODE : ".$awal." - ".$akhir,0,0,'L');


//membuat kop tabel    
$pdf->SetFont('Arial','B',11);
$pdf->SetXY(1.5,5);
$pdf->Cell(2,2, "No",1,0,'C');
$pdf->Cell(9,2, "Jenis Kendaraan",1,0,'C');
$pdf->Cell($width,1, "Data Stok Kendaraan Rental",1,2,'C');
	for($i=$awal; $i<=$akhir; $i++){
			$pdf->Cell(3,1, "$i",1,0,'C');
	}
	
$pdf->SetFont('Arial','',9);
$pdf->Ln();

$no = 1;
$sql = "SELECT DISTINCT a.tipekendaraan FROM gl_masterkendaraan as a inner join gl_tipekendaraan as b on (a.tipekendaraan = b.id)";
$query = mysql_query($sql) or die (mysql_error());

while(list($tipekend) = mysql_fetch_row($query)){
		$pdf->SetX(1.5);
		$sql2 = "SELECT tipekendaraan FROM gl_tipekendaraan WHERE id = '$tipekend'";
		list($namakend) = mysql_fetch_row(mysql_query($sql2));		
		
		$pdf->Cell(2,0.7, "$no",1,0,'C');
		$pdf->Cell(9,0.7, "$namakend",1,0,'C');
		for($i=$awal; $i<=$akhir; $i++){
			$sql3 = "SELECT COUNT(tahun) FROM gl_masterkendaraan WHERE tipekendaraan = '$tipekend' AND tahun = '$i'";
			list($jmltahun) = mysql_fetch_row(mysql_query($sql3)); 
			if($jmltahun == '0') $jmltahun = '';
			
			$pdf->Cell(3,0.7, "$jmltahun",1,0,'C');
		}
		
		$pdf->Ln();
		$no++;
}
$pdf->SetX(1.5);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(11,0.7, "Total",1,0,'C');

	$total = array();
	while(list($tipekend) = mysql_fetch_row($query)){
		list($namakend) = mysql_fetch_row(mysql_query("SELECT tipekendaraan FROM gl_tipekendaraan WHERE id = '$tipekend'"));		
		
		for($i=$awal; $i<=$akhir; $i++){
			list($jmltahun) = mysql_fetch_row(mysql_query("SELECT COUNT(tahun) FROM gl_masterkendaraan WHERE tipekendaraan = '$tipekend' AND tahun = '$i'")); 
			if($jmltahun == '0') $jmltahun = '';
			$total[$i] =$jmltahun;		
		}				
	
	}
	for($i=$awal; $i<=$akhir; $i++){
				list($jml) = mysql_fetch_row(mysql_query("SELECT COUNT(tahun) FROM gl_masterkendaraan WHERE tahun = '$i'")); 
			$pdf->Cell(3,0.7, "$jml",1,0,'C');
	}



$pdf->Ln();
$pdf->Output('Laporan Data unit Per Tahun Periode '.$awal." - ".$akhir.'.pdf','D');


?>